﻿// JScript File

LookupControl_GetValues = function()
{
	var values = new Array();
	
	var oXmlDoc = CreateXmlDocument(false);	
	var text = this.getItemXml();
	if(!IsNull(text) && text != "")
	{
		oXmlDoc.loadXML(text);
		var itemNodes = oXmlDoc.selectNodes("//Items/LookupItem");
		for (var i = 0; i < itemNodes.length; i++)
		{
			var oItem = new Object();
			
			var itemNode = itemNodes[i];
			var nameNode = itemNode.selectSingleNode("Name");			
			oItem.name = nameNode.text;
			var guidNode = itemNode.selectSingleNode("Guid");			
			oItem.id = guidNode.text;
			var typeNode = itemNode.selectSingleNode("TypeCode");			
			oItem.type = typeNode.text;
			var typeNameNode = itemNode.selectSingleNode("TypeName");			
			oItem.typename = typeNameNode.text;
			
			values[i] = oItem;
		}
	}
	
	return values;
}

LookupControl_SetValues = function(oItems, fireClientChange)
{
	var oXmlDoc = CreateXmlDocument();
	var oItemsElement = oXmlDoc.createElement("Items");
	oXmlDoc.documentElement = oItemsElement;
	
	for (var i = 0; i < oItems.length; i++)
	{
		var oItem = oItems[i];
		
		var oItemElement = oXmlDoc.createElement("LookupItem");
		oItemsElement.appendChild(oItemElement);
		
		var oNameElement = oXmlDoc.createElement("Name");
		oNameElement.text = oItem.name;
		oItemElement.appendChild(oNameElement);
		
		var oGuidElement = oXmlDoc.createElement("Guid");
		oGuidElement.text = oItem.id;
		oItemElement.appendChild(oGuidElement);
		
		var oTypeCodeElement = oXmlDoc.createElement("TypeCode");
		oTypeCodeElement.text = oItem.type;
		oItemElement.appendChild(oTypeCodeElement);
		
		var oTypeNameElement = oXmlDoc.createElement("TypeName");
		if(!IsNull(oItem.typename))
		{
			oTypeNameElement.text = oItem.typename;
		}
		oItemElement.appendChild(oTypeNameElement);
	}	
	var oItemXml = oItemsElement.xml;
	this.setItemXml(oItemXml);
	
	this.count = oItems.length;
	
	LookupControl_BuildItems(this, oItems);
	
	if(!IsNull(this.onClientChange) && (IsNull(fireClientChange) || fireClientChange == true))
	{
		this.onClientChange();
	}
}

LookupControl_RemoveValues = function(oItems)
{
	var values = new Array();
	
	var oXmlDoc = CreateXmlDocument(false);	
	var text = this.getItemXml();
	if(!IsNull(text) && text != "")
	{
		oXmlDoc.loadXML(text);
		
		var filter = "";
		
		for(var i = 0; i < oItems.length; i++)
		{
			if(filter != "")
			{
				filter += " and ";
			}
			filter += "Guid != '" + oItems[i].id + "'";
		}
		
		if(filter != "")
		{
			filter = "[" + filter + "]";
		}
		
		var itemNodes = oXmlDoc.selectNodes("//Items/LookupItem" + filter);
		
		for (var i = 0; i < itemNodes.length; i++)
		{
			var oItem = new Object();
			
			var itemNode = itemNodes[i];
			var nameNode = itemNode.selectSingleNode("Name");			
			oItem.name = nameNode.text;
			var guidNode = itemNode.selectSingleNode("Guid");			
			oItem.id = guidNode.text;
			var typeNode = itemNode.selectSingleNode("TypeCode");			
			oItem.type = typeNode.text;
			var typeNameNode = itemNode.selectSingleNode("TypeName");			
			oItem.typename = typeNameNode.text;
			
			values[i] = oItem;
		}
		
	}
	
	this.setValues(values);
	
	return values;
}

LookupControl_GetIconUrl = function(oItem)
{
	return this['iconUrlFor' + oItem.type];
}

LookupControl_GetOpenUrl = function(oItem)
{
	var urlFormat = this['openUrlFormatFor' + oItem.type];
	var url = formatString(urlFormat, oItem.id);
	return url;
}

LookupControl_BrowseForLookup = function(ctrlLookup)
{
	var crmOrganization = ctrlLookup.orgName;
    var lookupClass = ctrlLookup.lookupClass;
    var allowMultiSelect = ctrlLookup.allowMultiSelect;
    
    if(IsNull(ctrlLookup.count))
    {
		var items = ctrlLookup.getValues();
		ctrlLookup.count = items.length;
    }
    
    var url = ctrlLookup.lookupDialogUrl + "?orgname=" + crmOrganization + "&class=" + lookupClass + "&multiselect=" + allowMultiSelect;
    
    if(ctrlLookup.count > 0)
    {
		url += "&enableremove=1";
    }
    
    for(var I = 0; I < ctrlLookup.queryStrings.length; I++)
    {
        var nameValuePair = ctrlLookup.queryStrings[I];
        url += "&" + nameValuePair.Name + "=" + nameValuePair.Value;
    }
    
    var returnValue = window.showModalDialog(url, null, "dialogHeight:480px;dialogWidth:640px;center:yes;resizable:yes");
    
    var returnCode = 1;
    
    if(!IsNull(returnValue))
    {
        returnCode = returnValue.exitcode;
        if(returnValue.exitcode == 0)
        {
		    ctrlLookup.setValues(returnValue.data);
        }
        else if(returnValue.exitcode == 2)
        {
		    ctrlLookup.removeValues(returnValue.data);
        }
    }
    
    return returnCode;
}

function LookupControl_BuildItems(lookup, items/*, urlFormat*/)
{
	var html = "";
	var len = items.length;
	
	for (var i = 0; i < len; ++i)
	{
	var item = items[i];

	html += (i > 0 ? " " : "");

	var e = document.createElement("SPAN");

	e.className	= lookup.itemCssClass;
	e.oid		= item.id;
	e.otype		= item.type;
	
	var icon = "<img class='" + lookup.itemCssClass + "' src='" + lookup.getIconUrl(item) + "'/>";
	e.innerHTML	= icon + item.name;
	
	e.onclick	= "OpenWindow('" + lookup.getOpenUrl(item) + "')";

	html += e.outerHTML;
	}
	if (html.length == 0)
	{
	html = "&nbsp;";
	}
	
	lookup.divLookup.innerHTML = html;
}

function LookupControl_KeyDown(lookup)
{
    if(window.event)
    {
        if(window.event.keyCode == 46 || window.event.keyCode == 8)
        {
            if(lookup.getValues().length > 0)
            {
                var items = new Array();
                lookup.setValues(items);
            }
            window.event.returnValue = false;
        }
    }
}
